package com.jvyou.driver.domain.modal;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.jvyou.driver.enums.AppHttpCodeEnum;
import lombok.Getter;
import lombok.Setter;

import java.io.Serial;
import java.io.Serializable;

/**
 * @author 橘柚
 * @version 1.0-SNAPSHOT
 * @Date 2024/8/12 10:03
 * @Description 统一接口相应类
 */
@Setter
@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = -8412929920074917076L;
    // Getter和Setter方法
    private Integer code;
    private String msg;
    private T data;

    public Result() {
        this(AppHttpCodeEnum.SUCCESS);
    }

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(Integer code, T data) {
        this(code, null, data);
    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(AppHttpCodeEnum enums) {
        this(enums.getCode(), enums.getMsg());
    }

    public Result(AppHttpCodeEnum enums, String msg) {
        this(enums.getCode(), msg);
    }

    // 静态工厂方法
    public static <T> Result<T> errorResult(int code, String msg) {
        return new Result<>(code, msg);
    }

    public static <T> Result<T> okResult() {
        return new Result<>(AppHttpCodeEnum.SUCCESS);
    }

    public static <T> Result<T> okResult(int code, String msg) {
        return new Result<>(code, msg);
    }

    public static <T> Result<T> okResult(T data) {
        Result<T> result = new Result<>(AppHttpCodeEnum.SUCCESS);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> errorResult(AppHttpCodeEnum enums) {
        return new Result<>(enums);
    }

    public static <T> Result<T> errorResult(AppHttpCodeEnum enums, String msg) {
        return new Result<>(enums, msg);
    }

    // 设置HTTP状态码和消息的便捷方法
    public static <T> Result<T> setAppHttpCodeEnum(AppHttpCodeEnum enums) {
        return new Result<>(enums);
    }

    public static <T> Result<T> setAppHttpCodeEnum(AppHttpCodeEnum enums, String msg) {
        return new Result<>(enums, msg);
    }

}